//
//  DateViewController.m
//  1243
//
//  Created by 我的 on 16/4/8.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "DateViewController.h"
#import "FDCalendar.h"

@interface DateViewController ()<FDCalendarDelegate>

@property (nonatomic, strong) FDCalendar *startCalendar;
@property (nonatomic, strong) FDCalendar *endCalendar;
@property (nonatomic, strong) NSString *selectedTime;
@property (nonatomic, assign) NSInteger num;

@end

@implementation DateViewController

- (FDCalendar *)startCalendar {
    
    if (!_startCalendar) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        
        _startCalendar = [[FDCalendar alloc] initWithCurrentDate:[NSDate date]];
        _startCalendar.delegate = self;
        
        
    }
    return _startCalendar;
}

- (FDCalendar *)endCalendar {
    
    if (!_endCalendar) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        
        _endCalendar = [[FDCalendar alloc] initWithCurrentDate:[NSDate date]];
        _endCalendar.delegate = self;
        
    }
    return _endCalendar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}

- (void)showDateView:(NSInteger)num {
    self.num = num;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.startCalendar.y = 154;
    self.startCalendar.x = (WIDTH - self.startCalendar.width) / 2;
    self.endCalendar.y = 154;
    self.endCalendar.x = (WIDTH - self.endCalendar.width) / 2;
    if (self.num == 10000) {
        [self.view addSubview:self.endCalendar];
    } else {
        [self.view addSubview:self.startCalendar];
    }
    self.view.size = window.size;
    self.view.y = HEIGHT;
    [window addSubview:self.view];
    [UIView animateWithDuration:0.5 animations:^{
        
        
        self.view.y = 0;
    } completion:^(BOOL finished) {
        
        
    }];
}

- (void)backDateView:(NSInteger)num {
    if (num == 10000) {
        self.selectedTime = [self.endCalendar getSelectedDate];
    } else {
        self.selectedTime = [self.startCalendar getSelectedDate];
    }
    NSLog(@"-----%@", self.selectedTime);
    [UIView animateWithDuration:0.5 animations:^{
        self.view.y = HIGHT;
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self backDateView:self.num];
    if (self.num == 10000) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changBtnSelected" object:nil userInfo:@{@"SelectedTime":self.selectedTime, @"BtnTag":[NSString stringWithFormat:@"%ld", self.num]}];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changBtnSelected" object:nil userInfo:@{@"SelectedTime":self.selectedTime, @"BtnTag":[NSString stringWithFormat:@"%ld", self.num]}];
    }
}

- (void)calendarItem:(FDCalendarItem *)item didSelectedDate:(NSDate *)date {
    [self backDateView:self.num];
    if (self.num == 10000) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changBtnSelected" object:nil userInfo:@{@"SelectedTime":self.selectedTime, @"BtnTag":[NSString stringWithFormat:@"%ld", self.num]}];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changBtnSelected" object:nil userInfo:@{@"SelectedTime":self.selectedTime, @"BtnTag":[NSString stringWithFormat:@"%ld", self.num]}];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
